package xj.graph2d.tree;

import static xj.app.BaseApplicationFrame.getResourceString;

import javax.swing.tree.DefaultMutableTreeNode;

import xj.graph2d.DrawShape;
import xj.graph2d.GraphEdgeBase;
import xj.graph2d.GraphNodeBase;
import xj.graph2d.Group;
import xj.graph2d.TextShape;
import xj.graph2d.graph.GraphNode;
import xj.graph2d.shapes.BoxLabel;

public class DrawShapeTreeNode
  extends DefaultMutableTreeNode 
  implements xj.util.gui.TreeNodeToolTips {

  public DrawShapeTreeNode(DrawShape shape) {
    this.shape = shape;
  }

  @Override
  public String toString() {
    if (shape != null) {
      String displayName = shape.getDisplayName();
      if (displayName == null) {
	// displayName.length() == 0) {
	String tempName = shape.getTemplateName();
	if (tempName != null) {
	  displayName = getResourceString("template." + tempName + ".title");
	  if (displayName == null) {
	    int i = tempName.lastIndexOf(".");
	    if (i >= 0) {
	      displayName = tempName.substring(i + 1);
	    }
	  }
	} else {
	  displayName = shape.getName();
	}
      }
      if (displayName == null) {
	displayName = "";
      }

      if (shape instanceof TextShape) {
	TextShape text = (TextShape) shape;
	String s = text.getWholeString();
	return displayName + " \"" + (s != null ? s : "") + "\"";
      } else if (shape instanceof BoxLabel) {
	/*
         * BoxLabel label = (BoxLabel) shape; String text = label.getText();
         * //return shape.toString() + " \"" + (text == null ? "" : text) +
         * "\""; return shape.toString();
         */
	return displayName;
      } else if (shape instanceof Group) {
	// return shape.toString();
	return displayName;
      } else if (shape instanceof GraphNodeBase) { 
	GraphNodeBase node = (GraphNodeBase) shape; 
	String name = node.getNodeDisplayName(); 
	if (name != null) { 
	  name = name.trim();
	  if (name.length() > 0) { 
	    displayName += (": " + name);
	  }
	}
	return displayName;
      } else if (shape instanceof GraphEdgeBase) {
	GraphEdgeBase edge = (GraphEdgeBase) shape;
	displayName += ": ";
	String name = edge.getEdgeDisplayName(); 
	if (name != null) { 
	  name = name.trim();
	  if (name.length() > 0) { 
	    displayName += (name + " ");
	  }
	}
	String srcString = "-";
	GraphNode src0 = edge.getSource();
	if (src0 != null) { 
	  srcString = null;
	  if (src0 instanceof GraphNodeBase) { 
	    GraphNodeBase src = (GraphNodeBase) src0;
	    String sname = src.getNodeDisplayName();
	    if (sname != null) { 
	      sname = sname.trim();
	      if (sname.length() > 0) { 
		srcString = sname;
	      }
	    }
	    if (srcString == null) { 
	      srcString = getTemplateDisplayname(src); 
	    }
	  }
	  if (srcString == null) { 
	    srcString = "?";
	  }
	}
	String desString = "-";
	GraphNode des0 = edge.getDestination();
	if (des0 != null) { 
	  desString = null;
	  if (des0 instanceof GraphNodeBase) { 
	    GraphNodeBase des = (GraphNodeBase) des0;
	    String dname = des.getNodeDisplayName();
	    if (dname != null) { 
	      dname = dname.trim();
	      if (dname.length() > 0) { 
		desString = dname;
	      }
	    }
	    if (desString == null) { 
	      desString = getTemplateDisplayname(des); 
	    }
	  }
	  if (desString == null) { 
	    desString = "?";
	  }
	}
	return displayName + "(" + srcString + ", " + desString + ")";
      } else {
	/*
         * String pre = ""; if (shape instanceof GraphNodeBase) { GraphNodeBase
         * node = (GraphNodeBase) shape; String id = node.getId(); if (id !=
         * null) { pre = id + ": "; } } else if (shape instanceof GraphEdgeBase) {
         * GraphEdgeBase edge = (GraphEdgeBase) shape; String id = edge.getId();
         * if (id != null) { pre = id + "=" + edge.getEdgeString() + ": "; } }
         * 
         * return displayName + " " + shape.getBoundsString() + " " + pre;
         */

	return displayName + " " + shape.getBoundsString();
      }
      // return shape.toString();
    } else {
      return null;
    }
  }

  public DrawShape getShape() {
    return shape;
  }

  public String getTip() {
    if (shape instanceof TextShape) {
      TextShape text = (TextShape) shape;
      return "\"" + text.getString() + "\"";
    } else {
      return toString();
    }
  }

  protected static String getTemplateDisplayname(DrawShape shape) {
    String displayName = null;
    if (shape != null) { 
      String tempName = shape.getTemplateName();
      if (tempName != null) {
	displayName = getResourceString("template." + tempName + ".title");
	if (displayName == null) {
	  int i = tempName.lastIndexOf(".");
	  if (i >= 0) {
	    displayName = tempName.substring(i + 1);
	  }
	}
      } 
    }
    return displayName;
  }

  protected DrawShape shape;

}
